package cn.edu.qfnu.oj.entity;

import cn.edu.qfnu.oj.entity.enums.SubmitState;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;

@Entity
public class Submit implements Serializable {

    private int id; // 提交id
    private String code;
    private SubmitState submitState; // 提交状态
    private Integer timeCost; // 时间消耗
    private Integer memoryCost; //内存占用
    private String language; //使用语言
    private LocalDateTime submitTime; //提交时间
    private Contest contest;  //隶属于的比赛
    private Task task; //隶属于的作业
    private Student student; //提交者
    private Problem problem; //隶属于的问题

    public Submit() {}

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "submit_state")
    public @Enumerated(value = EnumType.STRING) SubmitState getSubmitState() {
        return submitState;
    }

    public void setSubmitState(SubmitState submitState) {
        this.submitState = submitState;
    }

    @Column(name = "time_cost")
    public Integer getTimeCost() {
        return timeCost;
    }

    public void setTimeCost(Integer timeCost) {
        this.timeCost = timeCost;
    }

    @Column(name = "memory_cost")
    public Integer getMemoryCost() {
        return memoryCost;
    }

    public void setMemoryCost(Integer memoryCost) {
        this.memoryCost = memoryCost;
    }

    @Column(name = "language", length = 16)
    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    @Column(name = "submit_time")
    public LocalDateTime getSubmitTime() {
        return submitTime;
    }

    public void setSubmitTime(LocalDateTime submitTime) {
        this.submitTime = submitTime;
    }

    @Column(name = "submit_code")
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        Submit submit = (Submit) o;

        return new EqualsBuilder()
                .append(id, submit.id)
                .append(submitState, submit.submitState)
                .append(timeCost, submit.timeCost)
                .append(memoryCost, submit.memoryCost)
                .append(language, submit.language)
                .append(submitTime, submit.submitTime)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(id)
                .append(submitState)
                .append(timeCost)
                .append(memoryCost)
                .append(language)
                .append(submitTime)
                .toHashCode();
    }

    //一个提交记录只对应一个比赛，而一个比赛可以由多次提交记录
    //因此提交记录与比赛是多对一不安息
    @ManyToOne // 维护提交记录与比赛的多对一关系
    @JoinColumn(name = "contest_id", referencedColumnName = "id")
    public Contest getContest() {
        return contest;
    }

    public void setContest(Contest contestByContestId) {
        this.contest = contestByContestId;
    }

    //一个提交记录只对应一次作业，而一次作业可以由多次提交记录
    //因此提交记录与作业是多对一关系
    @ManyToOne // 维护提交记录与作业的多对一关系
    @JoinColumn(name = "task_id", referencedColumnName = "id")
    public Task getTask() {
        return task;
    }

    public void setTask(Task taskByTaskId) {
        this.task = taskByTaskId;
    }

    //一个提交记录只对应一个学生，而一个学生可以有多次提交记录
    //因此提交接记录与学生是多对一关系
    @ManyToOne // 维护提交记录与学生的多对一关系
    @JoinColumn(name = "student_id", referencedColumnName = "student_id")
    public Student getStudent() {
        return student;
    }

    public void setStudent(Student submits) {
        this.student = submits;
    }

    //一个提交记录只对应一个问题，而一个问题可以有多次提交记录
    //因此提交记录与问题是多对一关系
    @ManyToOne // 维护提交记录与问题的多对一关系
    @JoinColumn(name = "problem_id", referencedColumnName = "id")
    public Problem getProblem() {
        return problem;
    }

    public void setProblem(Problem problemByProblemId) {
        this.problem = problemByProblemId;
    }

}
